<?php

namespace sframe;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;

class ExceptionHandler
{
	/**
	 * @param $exception
	 * @param bool $debug
	 * @return Response
	 */
	public static function process($exception, $debug = false)
	{
		self::record($exception, $debug);

		return self::notify($exception, $debug);
	}

	/**
	 * 记录异常日志
	 * @param $exception
	 * @param bool $debug
	 * @param string $tag
	 */
	public static function record($exception, $debug = false, $tag = "ExceptionHandler")
	{
		// 非调试模式下 才记录日志
		if (!$debug) {
			put_file_contents($exception, $tag);
		}
	}

	/**
	 * 自定义错误显示页面
	 * @param $exception
	 * @param bool $debug
	 * @return Response
	 */
	public static function notify($exception, $debug = false)
	{
		if (!$debug) {

			if ($exception instanceof ResourceNotFoundException) {
				return new Response("Page Not Found", 200);
			}

			return new Response("Internal Server Error", 200);
		}

		throw $exception;
	}
}
